/**
 * @fileOverview common functions needed throughout the site
 * @name Common
 * @copyright 2007-2008 Joe Cianflone and JoeCianflone.com, All Rights Reserved.  
 * @author Joe Cianflone
 */


/**
 * @class this is the Page class
 */
var Page = Class.create();
Page.prototype = {
	/**
	 * simple class of functions needed on many pages of websites
	 * @constructor
	 * @param {Hash} [options]
	 * @config {string} value
	 */
	initialize:function(options) {
		this.options = $H(options);
		/*this.getMetaSig = this.setMetaSig();
		this.getBodySig = this.setBodySig(); */
		
		this.externalLinks();
	},
	/**
	 * 
	 */
	validate:function(formID, options) {
		
		if (typeof(options) == null) {
			new Validation(formID, {
				immediate: false,
				focusOnError : true,
				useTitles : true,
				onSubmit : true
			});
		}//if...
		else {
			options = $H(options);
			new Validation(formID, {
				immediate : options.get('immediate'),
				onFormValidate : options.get('validateFunction'),
				focusOnError : true,
				useTitles : true,
				onSubmit : true
			});
		}//else...
		
		
	},
	/**
	 * 
	 * @param {Object} toggleClass
	 */
	toggle:function(toggleClass) {
		this.toggleClass = $$('.'+toggleClass);
		for (var i=0, len=this.toggleClass.length; i < len; ++i) {
			Event.observe (this.toggleClass[i], 'click', this.toggler.bind(this), false);
		}
	},	
	/**
	 * Uses Script.aculo.us effects to toggle a child element open and closed.  
	 * @param {Object} evt this is an object that represents the event that occured and the element it occured on NOTE: prototype event observers automatically pass this parameter.  You don't need to create it anywhere.
	 */
	toggler:function(evt) {
		var element = Event.element(evt);
		var toggler = $(element.id).next();
		if (toggler == null) {
			toggler = $(element.id).up();
			toggler = toggler.next();
		}//if...
		Effect.toggle(toggler, 'blind', {duration:1});			
	},
	/**
	 * A more elegant wrapper class for SWFObject.  Makes loading multiple movies with
	 * multiple parameters easier and helps to encapsulate the SWFObject code.  
	 * @param {Object} params required parameters that SWFObject needs to work
	 * @config {string} [src] location of your movie
	 * @config {string} [name] name of your movie
	 * @config {string} [width]width
	 * @config {string} [height]height
	 * @config {string} [version]lowest compatabile version of flash you want to work with
	 * @config {string} [bgColor]background color if you movie doesnt fill the div
	 * @config {string} [divID] || 'flashcontent' the HTML ID that the movie should appear in 
	 * @config {string} [swfVars]
	 * @param {Hash} [options] optional values the user defines and passes into the flash movie as needed
	 * @example Event.observe(window, 'load', function(){
	 * var currentPage = new Page();
	 * currentPage.startMovie(
	 * 		{
	 * 			src: '../url/to/movie.swf',		
	 * 			name: 'Movie Name',				
	 * 			width: '640',					
	 * 			height: '480',					
	 * 			version: '8',					
	 * 			bgcolor: '#fff'					
	 * 			divID: 'movie'					//over riding the flashcontent DIV name
	 * 		}, 
	 * 		{
	 * 			myVar: 'something I am setting'	//variable that is needed in the flash movie
	 * 		}); 					
	 * }, false);
	 * @exception startMovie Exception
	 */	
	startMovie:function(params, options) {
		try {
			var so = new SWFObject(params.get('src'),params.get('name'), params.get('width'), params.get('height'), params.get('version'), params.get('bgColor'));
			try {
				options = $H(options);
				options.each(function(pairs) {
					so.addVariable(pairs.key.toString(), pairs.value);
				});//options.each()...
			}//try...
			catch (e) {}//catch...
			try {
				so.write(params.get(divID));	
			}//try...
			catch (e) {
				so.write('flashcontent');	
			}//catch...
		}//try...
		catch (e) {
			alert ("Sorry, it seems there has been an internal error in StartMovie.");
		}//catch...		
		var undertext = $$('.flashcontent_text');
		for (i=0, len=undertext.length; i < len; ++i) {
			undertext[i].style.display="block";
		}//for...
	},
	startMovie_temp: function (movieInfo, params, variables) {
		try {
			var so = new SWFObject(movieInfo.get('src'),movieInfo.get('name'), movieInfo.get('width'), movieInfo.get('height'), movieInfo.get('version'), movieInfo.get('bgColor'));
			try {
				params = $H(params);
				params.each(function(pairs) {
					so.addParam(pairs.key.toString(), pairs.value);
				});//params.each()...
			}//try...
			catch (e) {}//catch...
			
			try {
				variables = $H(variables);
				variables.each(function(pairs) {
					so.addVariable(pairs.key.toString(), pairs.value);
				});//variables.each()...
			}//try...
			catch (e) {}//catch...		
			
			try {
				so.write(params.get(divID));	
			}//try...
			catch (e) {
				so.write('flashcontent');	
			}//catch...		
		}//try...
		catch(e) {
			alert ("Error Loading the Flash Movie: "+e);
		}//catch...
		
		var undertext = $$('.flashcontent_text');
		for (i=0, len=undertext.length; i < len; ++i) {
			undertext[i].style.display="block";
		}//for...				
	},
	/**
	 * The XHTML 1.0 spec does not allow for target='new' events.  This function remedies that.
	 */
	externalLinks:function() {
		try {
			var anchors = document.getElementsByTagName("a");
			for (var i=0, len=anchors.length; i < len; ++i) {
				if (anchors[i].getAttribute("rel") == "external") {
					anchors[i].target = "_blank";
				}//if...
			}//for...
		}//try...
		catch (e) {
			return 0;
		}//catch...		
	},
	/**
	 * Need to use a copyright date somewhere?  Just call this function and you get 
	 * a dynamic year.  
	 * @param {String} statement anything else you want to add in right after the date
	 */
	copyright:function(statement, loc) {
		var d = new Date();
		
		var childNode = document.createTextNode(d.getFullYear()+ " "+statement);
		
		if (typeof(loc) == null) {
			$('copyright').appendChild(childNode);
		}//if...
		else {
			$(loc).appendChild(childNode);	
		}//else..
				
	},
	/**
	 * gets the page signature, that is the ID that is set on the <body> tag
	 */
	pageSig:function() {
		return document.getElementsByTagName('body')[0].id;
	}
};//Page.prototype...

/**
 * @class this is the Rollover class
 */
var Rollovers = Class.create();
Rollovers.prototype = {
	/**
	 * @constructor
	 * @param {Object} rollers
	 * @param {Object} options
	 */
	initialize:function(rollers, options) {
		if (typeof(options) == null) {
			this.imageOptions = {
				path: '/images/buttons/',
				hoverExtension: '_selected',
				imagePrefix: '',
				selectedExtension: '_hover',
				imageExtension: '.gif'
			};
			this.imageOptions = $H(this.imageOptions);
		}//if...
		else {
			this.imageOptions = $H(options);
		}//else...

		this.ele = null;
		this.rollers = $$('.'+rollers);
		
		for (i=0, len=this.rollers.length; i < len; ++i) {
			Event.observe(this.rollers[i], 'mouseover', this.rollover.bind(this), false);
			Event.observe(this.rollers[i], 'mouseout', this.rollout.bind(this), false);
		}//for...
		
	},
	/**
	 * used in the mouseover state
	 * @param {Event} event
	 */
	rollover:function(event) {
		this.ele = Event.element(event);
		var parent = this.ele.up();
		
		var url = this.buildURL(this.imageOptions.get('path'), 
								this.imageOptions.get('imagePrefix'), 
								parent.id, 
								this.imageOptions.get('hoverExtension'), 
								this.imageOptions.get('imageExtension'));
	
		this.setURL(url);		
	},
	/**
	 * used in the mouseout state
	 * @param {Event} event
	 */
	rollout:function(event) {
		this.ele = Event.element(event);
		var parent = this.ele.up();
		
		if (parent.className == 'selected') {
			var url = this.buildURL(this.imageOptions.get('path'), 
									this.imageOptions.get('imagePrefix'), 
									parent.id, 
									this.imageOptions.get('selectedExtension'), 
									this.imageOptions.get('imageExtension'));
		}//if...
		else {
			var url = this.buildURL(this.imageOptions.get('path'), 
									this.imageOptions.get('imagePrefix'), 
									parent.id, 
									'', 
									this.imageOptions.get('imageExtension'));
		}//else...
		
		this.setURL(url);
	},
	/**
	 * 
	 */
	setSelected: function() {
		
	},
	/**
	 * sets the URL for a background image
	 * @param {string} url
	 */
	setURL: function (url) {
		this.ele.style.backgroundImage = 'url('+url+')';
	},
	/**
	 * correctly builds a URL with the supplied information
	 * @param {string} path
	 * @param {string} prefix
	 * @param {string} pid
	 * @param {string} extension
	 * @param {string} imageExtension
	 * @return {string} a correctly build URL
	 */
	buildURL: function(path, prefix, pid, extension, imageExtension) {
		return path + prefix + pid + extension + imageExtension;
	}
};

var XML = Class.create();
XML.prototype = {
	
	initialize: function(transport, root) {
		this.response = transport.responseXML;
		this.setXML(this.response); 
		this.root = root;
	},//initalize()...
		
	setXML: function(nodeSet) {
		this.xml = $A(nodeSet.getElementsByTagName(this.root)[0]);
	},//setXML()...
	getXML: function() {
		return this.xml;
	},//getXML()...
	
	getChildren: function(parent, child) {
		var the_parents =  this.getSiblings(parent);
		var results = new Array();
		
		for (var i = 0, len=the_parents.length; i < len; ++i) {
			results[i] = new Array();	
			results[i] = the_parents[i].getElementsByTagName(child);
		}//for...	
		
		return results;
	},//getChildren()...
	
	getAttribute: function(node, value) {
		return node.readAttribute(attr);
	},//getAttribute()...	
	getNodesWithAttribute: function() {
		
	},//getNodesWithAttribute()...
	getNodesWithValue: function() {
		
	},//getNodesWithValue()...
	
	setAttribute: function() {
		
	},//setAttribute()...
	setNodeValue: function() {
		
	},//setNodeValue()...
	
	getSiblings: function(parent) {
		var resultSet = new Array();
		return $A(this.XMLData.getElementsByTagName(parent));
	}//getSiblings()...
	
};//XML...

var SendAjax = Class.create();
SendAjax.prototype = {
	initialize: function(url) {
		this.url = url;
	}, 
	readMessage: function() {
		
	},
	fallback: function() {
		
	}
};
